//
//                                               Setup
//
//
void
setup ()
{

#if FASTADC
  //This seems to have a huge impact on allowable sampling rate
  // set prescale to 16
  sbi (ADCSRA, ADPS2);
  cbi (ADCSRA, ADPS1);
  cbi (ADCSRA, ADPS0);
#endif

  memset (seg7cache, 1, sizeof (seg7cache));
  Serial.begin (9600);
  Serial.println ("Initializing timerinterrupt");
  starttime = millis ();
  randomSeed (analogReadMulti (0));
  digitalWrite (10, 0);		//disable
  digitalWrite (12, 0);
  pinMode (12, OUTPUT);
  //  pinMode(13,  OUTPUT);
  digitalWrite (11, 0);
  pinMode (11, OUTPUT);
  digitalWrite (PWMENABLE, 0);	//disable pwm
  pinMode (PWMENABLE, OUTPUT);
  digitalWrite (PWMREVERSE, 0);	//disable pwm
  pinMode (PWMREVERSE, OUTPUT);
  digitalWrite (BRAKE_RELEASE, 0);
  pinMode (BRAKE_RELEASE, OUTPUT);
  digitalWrite (HEADLIGHTS, 0);
  pinMode (PWMPOWER, OUTPUT);
  digitalWrite (PWMPOWER, 0);
  pinMode (HEADLIGHTS, OUTPUT);
  pinMode (DEBUGLIGHT, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode (READFAULT, INPUT);
  pinMode (READHEADLIGHTS, INPUT);
  //  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
  // Setup the LCD
  myGLCD.initLCD ();
  myGLCD.clrScr ();
  myGLCD.setContrast (64);
  myGLCD.setColor (255, 0, 0);
  myGLCD.fillRect (0, 0, 127, 9);
  myGLCD.setColor (64, 64, 64);
  myGLCD.fillRect (0, 117, 127, 127);
  myGLCD.setColor (0, 255, 0);
  myGLCD.drawRect (0, 10, 127, 116);
  //  printSevens(0,0);
  //  printSevens(-888,1);
  //  delay(100);

  int bcolor = 64;		//grey for bottom display
  int color = 255;		//grey for bottom display
  myGLCD.setColor (color, color, color);
  myGLCD.print ("    Batt V", LEFT, TOP, bcolor, 0, bcolor);
  myGLCD.print ("     Rabbit", LEFT, BOTTOM, bcolor, bcolor, bcolor);
  // timer 1 controls the PWM rate.  100 (about 10khz) was fastenough that I couldn't hear it.
  // higher rates increase switching loss
  // because the currents are fairly high, the noise caused by lower rates.
  // I bet dogs will hear this
  Timer1.initialize (100);	// initialize timer1 (PWM rate)
  // 5ms seemed to work.  Its 200hz which seemed slow for current loop
  // however we are doing current limiting, not control
  // Its also important to realize that the display messes this up anyway
  // So I don't think you gain anything by going faster.
  Timer3.initialize (4000);	// initialize timer3 (PWM control)
  //these don't really help things, but I don't like uninitilized variables
  Timer1.pwm (PWMA, 0);		// setup pwm on pin 11, 0% duty cycle
  Timer1.pwm (PWMB, 0);		// setup pwm on pin 12, 0% duty cycle
  Timer3.attachInterrupt (callback);	// start the control thread
  //the display thread is started when we exit
  //for(;;)
  //DisplayLoop();
}












